package com.hsy.dormmanage.controller;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hsy.dormmanage.common.result.Result;
import com.hsy.dormmanage.model.Student;
import com.hsy.dormmanage.service.CheckSer;
import com.hsy.dormmanage.service.StudentSer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * 包名:com.hsy.dormmanage.controller
 * 用户:86182
 * 时间:20211116:412021/11/13
 * 管理员功能:
 *
 */
@Api(value = "admin")
@RestController
@RequestMapping("/dorm/admin")
public class AdminController {
    @Autowired
    private StudentSer studentSer;

    @Autowired
    private CheckSer checkSer;

    //查询所有学生信息,分页
    @ApiOperation("查询所有学生信息")
    @ApiImplicitParam(value = "currentPage",name = "当前页")
    @GetMapping("/queryall")
    public Result queryAllStudent(@RequestParam(value = "currentPage",defaultValue = "1") Integer currentPage){

        Page<Student> stuPage = new Page<>(currentPage,20L);
        Page<Student> studentPage = studentSer.page(stuPage);
        List<Student> studentList = studentPage.getRecords();

        return Result.ok(studentList);
    }

    //查询子列表,所有学生信息，树形查询
    /**
     * 在每个父结点，点击后查询
    * */
    @ApiOperation("查询某楼栋学生信息")
    @ApiImplicitParam(name = "id",value = "父结点的id",dataType = "long",paramType = "query")
    @GetMapping("/qureyChildren")
    public Result qureyChildren(@RequestParam long id){
        List<Student> students = studentSer.queryClockStudent(id);
        return Result.ok(students);
    }
    //未打卡人员
    @ApiOperation("查看某栋楼未打卡人员")
    @ApiImplicitParam(name = "id",value = "楼栋的id",dataType = "long",paramType = "query")
    @GetMapping("/noCheck")
    public Result queryNoCheck(@RequestParam long id){
        List<Student> students = studentSer.queryNoClockStudent(id);
        return Result.ok(students);
    }

    //得到疑似人员比
    @ApiOperation(value = "返回疑似人员与所有人员")
    @GetMapping("/suspected")
    public Result queryIsSuspected(){
        int todaySuspected = checkSer.isSuspected();
        int allSuspected = studentSer.querySus();
        return Result.ok(new int[]{todaySuspected,allSuspected});
    }

    //添加学生
    @ApiOperation(value = "添加学生")
    @PutMapping ("/savestundent")
    public Result addStudent(Student student){

        boolean b = studentSer.save(student);
        return Result.ok(b);
    }

    //修改学生信息
    @ApiOperation(value = "修改学生")
    @GetMapping("/modifyStudent")
    public Result modfiyStudent(Student student){
        boolean b = studentSer.updateById(student);
        return Result.ok(b);
    }
    //删除一个学生
    @ApiOperation(value = "删除学生")
    @DeleteMapping ("/deleteStudent")
    public Result deleteStudent(@RequestParam long id){

        boolean b = studentSer.removeById(id);
        return Result.ok(b);
    }

    //导出学生信息
    @ApiOperation(value = "导出清单", notes = "export", produces = "application/octet-stream")
    @GetMapping("/outputStudents")
    public void outPutStudentList(HttpServletResponse response) throws UnsupportedEncodingException {

        studentSer.outPutStudentList(response);
        //return Result.ok();
    }

    //批量删除
    @ApiOperation(value = "批量删除")
    @ApiImplicitParam(name = "id",value = "要删除的id",dataType = "List",paramType = "query")
    @GetMapping("removebatch")
    public Result removeStudents(@RequestParam List<Long> ids){
        studentSer.removeByIds(ids);
        return Result.ok();
    }
}
